from odoo import fields, models, api
from odoo.exceptions import ValidationError


class ProductCategory(models.Model):
    _inherit = 'product.category'

    s_code = fields.Char(string='Code', copy=False)
    s_use_expiration_date = fields.Boolean(
        string='Use Expiration Date',
        help='When this box is ticked, you have the possibility to specify dates to manage'
             ' product expiration, on the product and on the corresponding lot/serial numbers')
    s_expiration_time = fields.Integer(
        string='Expiration Date',
        help='Number of days after the receipt of the products (from the vendor'
             ' or in stock after production) after which the goods may become dangerous'
             ' and must not be consumed. It will be computed on the lot/serial number.')
    s_use_time = fields.Integer(
        string='Best Before Date',
        help='Number of days before the Expiration Date after which the goods starts'
             ' deteriorating, without being dangerous yet. It will be computed on the lot/serial number.')
    s_removal_time = fields.Integer(
        string='Removal Date',
        help='Number of days before the Expiration Date after which the goods'
             ' should be removed from the stock. It will be computed on the lot/serial number.')
    s_alert_time = fields.Integer(
        string='Alert Date',
        help='Number of days before the Expiration Date after which an alert should be'
             ' raised on the lot/serial number. It will be computed on the lot/serial number.')

    @api.constrains('s_code')
    def constrains_s_code(self):
        for rec in self:
            if self.search_count([('s_code', '=', rec.s_code)]) > 1:
                raise ValidationError('The product category code must be unique!')

    def is_parent_category(self, child):
        if not self or not child:
            return False
        # 检查 self是否是child的父级
        return str(self.id) in child.parent_path.split('/')
